﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDMAPI
{
    public static class StringExtension
    {
        /// <summary>
        /// 全局加密ＫＥＹ
        /// </summary>
        public static string GlobalSlat
        {
           
            get {

                return "Ygzmle0YQ71xSs9O4KnRUTMX1av_w_lRrE2KelNm";

                // 引用： System.Configuration 命名空间，读取Web.Config 配置文件
                // return System.Configuration.ConfigurationManager.AppSettings["PMS_SETTING_GLOBAL_SALT"];
            }
        }
        /// <summary>
        /// 根据盐值生产密码
        /// </summary>
        /// <param name="pwd">密码明文</param>
        /// <param name="userSalt">用户的盐值 即：UserInfo表中 Salt 字段，随机生成</param>
        /// <returns></returns>
        public static string HashSaltPassword(this string pwd, string userSalt)
        {
            //SHA1(userpassword + globalsalt + usersalt)
            //加密算法：  SHA1(密码+全局加密KEY+每个用户特有秘钥KEY)

            return (pwd + GlobalSlat + userSalt).Sha1();
        }
        /// <summary>
        /// SHA1
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        public static string Sha1(this string input)
        {
            using (var sha = System.Security.Cryptography.SHA1.Create())
            {
                var bytes = Encoding.UTF8.GetBytes(input);
                var hash = sha.ComputeHash(bytes);
                return Convert.ToBase64String(hash);
            }
        }
    }
}
